// pages/caseStudy/caseStudy.js
import CaseStudyApi from '../../api/caseStudy.js'
import CaseStudyTestApi from "../../api/caseStudyTest";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        show: false,
        viewHeight: 0,
        letterScrolltop:0,
        screenData: {
            dept: {
                list: []
            },
            sex: {
                list: [
                    {name: '不限', id: '0', checked: true},
                    {name: '男', id: '1', checked: false},
                    {name: '女',id: '2',checked: false}
                ]
            },
            age: {
                list: [
                    {name: '不限', id: '0', checked: true},
                    {name: '青年', id: '1', checked: false, age: {maxAge: 35, minAge: 15}},
                    {name: '中年',id: '2',checked: false,age: {maxAge: 36, minAge: 60}},
                    {name: '老年', id: '3', checked: false,age: {maxAge: 150, minAge: 61}}
                ]
            },
            difficulty: {
                list: [
                    {name: '不限', id: '0',num:0, checked: true},
                    {name: '1星', id: '1',num:1, checked: false},
                    {name: '2星',id: '2',num:2,checked: false},
                    {name: '3星', id: '3',num:3, checked: false}
                ]
            }
        },
        caseList:[],
        caseItemHeight:0,
        caseNum:1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDepartmentList()
        this.getCaseList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // 页面首次渲染完毕时执行
        let height = wx.getSystemInfoSync().windowHeight
        let $navBar = this.selectComponent("#navBar")
        let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
        let viewHeight = height - navBarHeight
        this.setData({
            viewHeight: viewHeight
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    showPopup() {
        this.setData({show: true});
    },

    onClose() {
        this.setData({show: false});
    },
    startStudy(e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/caseStudyTest/caseStudyTest?id=${id}`
        })
    },
    goToStudyLogger() {
        wx.navigateTo({
            url: '/pages/studyLogger/studyLogger'
        })
    },
    /**
     * 专项
     */
    handlerGobackClick() {
        wx.redirectTo({
            url: '/pages/home/home'
        });
    },
    /**
     * 查询病例列表
     */
    async getCaseList() {
        const screenData = this.data.screenData
        const spDepartmentId = []
        screenData.dept.list.forEach((item, index) => {
            if (index != 0 && item.checked) {
                spDepartmentId.push(item.id)
            }
        })
        const gender = []
        screenData.sex.list.forEach((item, index) => {
            if (index != 0 && item.checked) {
                gender.push(Number(item.id))
            }
        })
        const hard = []
        screenData.difficulty.list.forEach((item, index) => {
            if (index != 0 && item.checked) {
                hard.push(Number(item.id))
            }
        })
        const age = []
        screenData.age.list.forEach((item, index) => {
            if (index != 0 && item.checked) {
                age.push(item.name)
            }
        })
        const param = {
            "gender": gender,
            "hard": hard,
            "likeKeyWord": this.data.value,
            "age":age,
            "spDepartmentId": spDepartmentId
        }
        const res = await CaseStudyApi.getCaseList(param)
        console.log(res)
        this.setData({
            caseList: res.data == null?[]:res.data
        })
        this.getContentHeight('#case-item-0')
    },
    /**
     * 筛选取消
     */
    screenCanal() {
        this.setData({
            show: false
        })
    },
    /**
     * 筛选确定
     */
    screenOk() {
        this.getCaseList()
        this.setData({
            show: false
        })
    },
    /**
     * 查询科室
     */
    async getDepartmentList() {
        const res = await CaseStudyApi.getDepartmentList()
        const screenData = this.data.screenData
        screenData.dept.list = res.data
        screenData.dept.list.unshift({name: '不限', id: '0', checked: true})
        this.setData({
            screenData: screenData
        })
    },
    /**
     * 筛选选项点击事件
     */
    screenItemChange(e) {
        const type = e.currentTarget.dataset.type
        const item = e.currentTarget.dataset.item
        const index = e.currentTarget.dataset.index
        const screenData = this.data.screenData
        if (index == 0) {
            screenData[type].list.forEach((item, i) => {
                item.checked = i == 0 ? true : false
            })
        } else {
            screenData[type].list[0].checked = false
            screenData[type].list[index].checked = !item.checked
        }
        this.setData({
            screenData: screenData
        })
    },
    /**
     * 初始化筛选数据
     */
    initScreenData() {

    },
    /**
     * 跳转到搜索
     */
    handlerGoSearch(){
        // wx.navigateTo({
        //     url: '/pages/searchCase/searchCase'
        // })
        let url = '/pages/searchCase/searchCase'
        if (getCurrentPages().length >= 10) {//判断当前页面栈是否大于等于10.如果大于或等于就使用wx.redirectTo来跳转页面，清除当前页面栈
            wx.redirectTo({url})
        } else {
            wx.navigateTo({url})
        }
    },
    /**
     * 滚动监听
     */
    scrollChange(e){
        const scrollTop = e.detail.scrollTop
        const num = parseInt(scrollTop/this.data.caseItemHeight) +1
        this.setData({
            caseNum : num
        })
    },
    /**
     * 返回顶部
     */
    back(){
        this.setData({
            letterScrolltop:0
        })
    },
    /**
     * 获取dom高度
     * @param {*} id
     */
    getContentHeight(id) {
        const query = wx.createSelectorQuery();
        const that = this;
        //选择id
        query
          .select(id)
          .boundingClientRect(function (rect) {
            console.log('rect',rect);// 这里包含内容 的宽高
            that.setData({
                caseItemHeight:rect.height + 12
            })
          })
          .exec();
      }
})
